作者:快乐xin_yi | 来源:互联网 | 2023-09-18 15:04
篇首语:本文由编程笔记#小编为大家整理,主要介绍了android绘制时钟,canvas学习相关的知识,希望对你有一定的参考价值。
自定义View,新建ClickView类继承View
import android .content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.view.View; import androidx.annotation.Nullable; public class ClickView extends View private Paint paint; //表盘外框 private Paint paint1_1; //表中心 private Paint paint2; //表数字 private Paint paint3;//表时针 private Paint paint4;//表分针 private Integer SHI &#61; 0 ;//时 private Integer FEN &#61; 0;//分 private Integer lining &#61; 100; //表盘半径 private Integer lining_sf &#61; 25; //表内径距离 public ClickView(Context context) super(context); public ClickView(Context context, &#64;Nullable AttributeSet attrs) super(context, attrs); public ClickView(Context context, &#64;Nullable AttributeSet attrs, int defStyleAttr) super(context, attrs, defStyleAttr); public ClickView(Context context, &#64;Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) super(context, attrs, defStyleAttr, defStyleRes); &#64;Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) super.onMeasure(widthMeasureSpec, heightMeasureSpec); //外部方法&#xff0c;设置时间并刷新 public void setSHIandFEN(Integer a,Integer b) this.SHI &#61; a; this.FEN &#61; b; //刷新 invalidate(); //绘制 public void onDraw(Canvas canvas) super.onDraw(canvas); //初始化 ini(); //画布移动到中心点 画表盘 canvas.translate(getWidth()/2,getHeight()/2); canvas.drawCircle(0,0,lining,paint); draw_ini(canvas); //画时针&#xff0c;分针 setFen(FEN,canvas); setShi(SHI,FEN,canvas); //画空心表中心 // canvas.drawOval(new RectF(-5,-5,5,5),paint); //画实心表中心 paint1_1 &#61; new Paint(); paint1_1.setColor(Color.RED); paint1_1.setAntiAlias(true); paint1_1.setStyle(Paint.Style.FILL_AND_STROKE); paint1_1.setStrokeWidth(3); canvas.drawCircle(0,0,10,paint1_1); //初始化笔刷 protected void ini() paint &#61; new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); paint2 &#61; new Paint(); paint2.setColor(Color.BLUE); paint2.setAntiAlias(true); paint2.setStyle(Paint.Style.STROKE); paint2.setStrokeWidth(1); paint3 &#61; new Paint(); paint3.setColor(Color.GREEN); paint2.setAntiAlias(true); paint3.setStyle(Paint.Style.STROKE); paint3.setStrokeWidth(3); paint4 &#61; new Paint(); paint4.setColor(Color.GREEN); paint4.setAntiAlias(true); paint4.setStyle(Paint.Style.STROKE); paint4.setStrokeWidth(2); //画表盘 &#xff0c;通过旋转画布实现 protected void draw_ini(Canvas canvas) canvas.rotate(180,0,0); for(int i&#61;0;i<60;i&#43;&#43;) if(i%5&#61;&#61;0) canvas.drawLine(0,lining,0,lining-12,paint); if(i&#61;&#61;0) canvas.drawText(String.valueOf(12),-4f, lining&#43;12f, paint2); else canvas.drawText(String.valueOf(i/5),-4f, lining&#43;12f, paint2); else canvas.drawLine(0,lining,0,lining-6,paint); canvas.rotate(6,0,0); //画时针分针&#xff0c;通过旋转画布实现 private void setShi(Integer SHI,Integer FEN,Canvas canvas) canvas.save(); if(SHI>&#61;12)SHI-&#61;12; if(SHI&#61;&#61;0||SHI&#61;&#61;12) canvas.drawLine(0,0,0,lining-lining_sf-20,paint3); else Integer degree &#61; SHI*30&#43;FEN/2; canvas.rotate(degree,0,0); canvas.drawLine(0,0,0,lining-lining_sf-20,paint3); canvas.restore(); private void setFen(Integer FEN,Canvas canvas) canvas.save(); if(FEN&#61;&#61;0||FEN&#61;&#61;60) canvas.drawLine(0,0,0,lining-lining_sf,paint4); else Integer degree &#61; FEN*6; canvas.rotate(degree,0,0); canvas.drawLine(0,0,0,lining-lining_sf,paint4); canvas.restore();
外部调用 xml文件
android:orientation&#61;"vertical" android:layout_width&#61;"match_parent" android:layout_height&#61;"match_parent" android:background&#61;"#fff" > //此处的包名按自己的来 android:id&#61;"&#64;&#43;id/click1" android:layout_width&#61;"300dp" android:layout_height&#61;"300dp" android:layout_marginRight&#61;"50dp"/> android:layout_width&#61;"wrap_content" android:layout_height&#61;"wrap_content" android:id&#61;"&#64;&#43;id/a1"/>
Button a1 &#61; findViewById(R.id.a1); a1.setOnClickListener(new View.OnClickListener() &#64;Override public void onClick(View v) //外部调用 ClickView view &#61; findViewById(R.id.a9); view.setSHIandFEN(10,51); );